/***********************************************************
*discription    下拉列表框、spinbox、进度条示例
*author         david
*date           2021-5-12
*modify         2021-5-12 8：30
************************************************************/
#include "mainWindow.h"

mainWindow::mainWindow()
{
	//QRect screenRect = QApplication::desktop()->screenGeometry();
	//this.setGeometry(100, 100, 800, 400);
	setWindowTitle("box类示例");
    /*****************************下拉列表***********************************/
    comboBox = new QComboBox;
    QStringList list;
    list<<"曾老大"<<"媞子"<<"李兴华"<<"张文青";
    //添加条目
    comboBox->addItems(list);
    //添加条目
    comboBox->addItem("魏杰");

    /*****************************spinbox************************************/
    spinBox = new QSpinBox;
    // 范围
    spinBox->setRange(20, 200);
    // 步长
    spinBox->setSingleStep(10);
    // 当前值
    spinBox->setValue(150);
    // 前缀
    spinBox->setPrefix("$ ");
    // 后缀
    spinBox->setSuffix(" %");
    // 开启循环
    spinBox->setWrapping(true);

    doubleSpinBox = new QDoubleSpinBox;
    //范围
    doubleSpinBox->setRange(0, 20);
    //精度
    doubleSpinBox->setDecimals(3);
    //步长
    doubleSpinBox->setSingleStep(0.005);

    /*****************************进度条************************************/
    progressBar = new QProgressBar;
    //打开资源文件
    QFile file(":/style/progressBar.qss");
    if (file.open(QIODevice::ReadOnly))
    {
        qDebug()<<"hehe";
        QString str = QString::fromLatin1(file.readAll());
        //给某个窗体设置样式表，progressBar为某个窗体对象
        progressBar->setStyleSheet(str);
        file.close();
    }
    //水平方向
    progressBar->setOrientation(Qt::Horizontal);
    //最小值
    progressBar->setMinimum(0);
    //最大值
    progressBar->setMaximum(4800);
    //当前进度
    progressBar->setValue(2000);
    double dProgress = (progressBar->value() - progressBar->minimum()) * 100.0
                    / (progressBar->maximum() - progressBar->minimum());
    progressBar->setFormat(QString::fromLocal8Bit("当前进度为：%1%").arg(QString::number(dProgress, 'f', 1)));
    //对齐方式
    progressBar->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

    /*****************************布局************************************/
	QVBoxLayout *vLay = new QVBoxLayout();
	QHBoxLayout *hLay = new QHBoxLayout();
    //添加控件
    hLay->addWidget(comboBox);
    hLay->addWidget(spinBox);
    hLay->addWidget(doubleSpinBox);
    hLay->addWidget(progressBar);
	vLay->addLayout(hLay);
    textEdit = new QTextEdit("textEdit");
    vLay->addWidget(textEdit);
	//整体布局
    QWidget *pWidget = new QWidget(this);
    pWidget->setLayout(vLay);
    setCentralWidget(pWidget);

    /*****************************连接信号和槽************************************/
    connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(comboBoxChangedSlot(int)));
    connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(spinBoxChangedSlot(int)));
    connect(spinBox, SIGNAL(valueChanged(QString)), this, SLOT(spinBoxChangedSlot(QString)));
    connect(doubleSpinBox, SIGNAL(valueChanged(QString)), this, SLOT(doubleSpinBoxChangedSlot(QString)));
}


void mainWindow::comboBoxChangedSlot(int index)
{
    textEdit->setText(comboBox->itemText(index));
}

void mainWindow::spinBoxChangedSlot(int value)
{
    textEdit->append("从控件获取数据spinBox->value：");
    textEdit->append(QString::number(spinBox->value()));
    textEdit->append("从函数获取参数value");
    textEdit->append(QString::number(value));
}
void mainWindow::spinBoxChangedSlot(QString str)
{
    textEdit->append("从控件获取数据spinBox->text:");
    textEdit->append(spinBox->text());
    textEdit->append("从函数获取参数text");
    textEdit->append(str);
}
void mainWindow::doubleSpinBoxChangedSlot(QString str)
{
    textEdit->append("从控件获取数据doubleSpinBox->value：");
    textEdit->append(QString::number(doubleSpinBox->value()));
}
void mainWindow::progressBarSlot()
{
	
}
